/**
 * Created by ruiyuan on 15-12-23.
 */
var groupService = require('../services/groupService');
var messageUtil = require('../common/utils/messageUtil');
var toolUtil = require('../common/utils/toolUtil');
var moment = require('moment');


exports.postUserContacts = function (req, res) {
    var user = req.user;
    var input = req.body;
    var deviceId = req.params.deviceId;
    var groupId = req.params.groupId;

    groupService.postUserContactsService(user, input, groupId, deviceId, function (err, result) {
        if (err) {
            logger.error('Failed to postUserContacts...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message, 8001));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result, 8001));
        }
    })
};

exports.createNewFamily = function (req, res) {
    var user = req.user;

    groupService.createNewFamilyService(user, function (err, result) {
        if (err) {
            logger.error('Failed to createNewFamily...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message, 8002));
        }
        else {
            res.json(messageUtil.composeSucceededMessage(req, null, result, 8002));
        }
    });
};

exports.getGroupList = function (req, res) {
    var user = req.user;

    groupService.getGroupListService(user, function (err, result) {
        if (err) {
            logger.error('Failed to getGroupList...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message, 8003));
        }
        else {
            res.json(messageUtil.composeSucceededMessage(req, null, result, 8003));
        }
    });
};

exports.getGroupInfo = function (req, res) {
    var groupId = req.params.groupId;
    var user = req.user;

    groupService.getGroupInfoService(user, groupId, function (err, result) {
        if (err) {
            logger.error('Failed to getGroupInfo...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message, 8004));
        }
        else {
            res.json(messageUtil.composeSucceededMessage(req, null, result, 8004));
        }
    })
};

exports.confirmToJoin = function (req, res) {
    var user = req.user;
    var input = req.body;

    groupService.confirmToJoinService(user, input, function (err, result) {
        if (err) {
            logger.error('Failed to confirmToJoin...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message, 8005));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result, 8005));

            toolUtil.putMessageBusinessMQ({
                msgType: 'group_join',
                toUserId: input.inviter,
                userId: user._id,
                groupId: input.groupId
            })

        }
    })
};

exports.quitFamily = function (req, res) {
    var user = req.user;
    var groupId = req.params.groupId;

    groupService.quitFamilyService(user._id, groupId, function (err, result) {
        if (err) {
            logger.error('Failed to quitFamily...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message, 8006));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result, 8006));
        }
    })
};

exports.inviteUser = function (req, res) {
    var user = req.user;
    var input = req.body;

    groupService.inviteUserService(user._id, input, function (err, result) {
        if (err) {
            logger.error('Failed to inviteUser...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message, 8007));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null, 8007));
        }
    })
};

exports.recordInvite = function (req, res) {
    var user = req.user;
    var input = req.body;

    groupService.recordInviteService(user, input, req.query, function (err, result) {
        if (err) {
            logger.error('Failed to recordInvite...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message, 8008));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result, 8008));
        }
    })
};

exports.getInviteInfo = function (req, res) {
    var groupId = req.params.groupId;
    var inviter = req.params.inviter;
    var user = req.user;

    groupService.getInviteInfoService(groupId, inviter, user, function (err, result) {
        if (err) {
            logger.error('Failed to getInviteInfo...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message, 8009));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result, 8009));
        }
    })
};